<?php

use app\widgets\Alert;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\bootstrap\ActiveForm;
use yii\grid\GridView;

$this->title = '数据库';
?>
<style>
    .middle {
        position: absolute;
        top: 40%;
        left: 50%;
        margin-left: -180px;
        transform: translateY(-50%);
        width: 360px;
    }
</style>

<div class="panel panel-default text-center">
    <div class="panel-heading">
        <h2>数据库更新管理</h2>
    </div>
    <div class="panel-body text-left">
        <!-- <?= Html::a('删除整个数据库', ['install/drop'], ['class' => 'btn btn-default']) ?> -->
        
        <!-- 提示信息 -->
        <div class="alert alert-warning">
            <h5>
                1. build.sql为初始化安装SQL，<code>无需更新</code>。<br>
                2. 每次更新数据库请在<code>project_path/db/</code>文件夹下新增一个单独的SQL文件。<br>
                3. 单独的SQL用<code>日期</code>来命名，如<pre>project_path/db/20171220.sql</pre>
                4. 所有增加的SQL文件将自动出现在下面的列表中，<code>选择查看SQL文件后再点击执行SQL</code>
            </h5>
        </div>
        
        <hr>

        <!-- SQL文件列表 -->
        <h2 class="text-center">数据库迁移列表</h2>
        <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'columns' => [
                // ['class' => 'yii\grid\SerialColumn'],

                'id',
                'filename',
                [
                    'attribute' => 'status',
                    'value' => function ($data) {
                        return $data['status'] == 0 ? '未更新' : '已更新';
                    }
                ],
                'env',
                'migrated_at:datetime',
                [
                    'class' => 'yii\grid\ActionColumn',
                    'template' => '{show} {up}',
                    'buttons' => [
                        'show' => function ($url, $model, $key) {
                            $url = Url::to(['install/show', 'id' => $model['filename']]);
                            $options = [
                                'title' => '查看',
                                'aria-label' => 'show',
                            ];
                            return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, $options);
                        },
                        'up' => function ($url, $model, $key) {
                            $url = Url::to(['install/run-update', 'id' => $model['filename']]);
                            $options = [
                                'title' => '更新',
                                'aria-label' => 'show',
                                'data' => [
                                    'confirm' => '请先检查您SQL语句，确认无误后更新',
                                    'method' => 'post',
                                ],
                            ];
                            return Html::a('<span class="glyphicon glyphicon-import"></span>', $url, $options);
                        },
                    ],
                    'visibleButtons' => [
                        'up' => function ($model, $key, $index) {
                            if ($model['filename'] == 'build') return false;
                            return ($model['status'] == 0) ? true : false;
                        }
                    ],
                ],        
            ],
        ]); ?>
    </div>
</div>
